<template>
  <div>
    <el-dialog v-dialogDrag :close-on-click-modal="true" class="" title="跳转逻辑" :visible.sync="open2" width="678px" append-to-body>
      <div class="flex_ac add_d pad_b20 pad_t10"><div class="dark">逻辑设置</div> <a href="javascript:;" class="color flex_ac margin_l20" @click="add_logic"><img src="@/assets/add2.png" alt="">添加逻辑设置</a></div>
      <el-scrollbar style=" width: 100%;">
        <div class="set_win_content" style="width: 100%; ">
          <el-form ref="form2" :model="form2" :rules="rules2">
            <div class="item margin_b10" v-for="(item,index) in logics">
              <div class="flex_ac">
                <div class="flex1 pad_r20">
                  <div class="s_item flex_ac">
                    <div style="width: 60px; text-align: right; padding-right:10px">如果</div>
                    <div class="s margin_r10" v-if="type!=3">
                      <el-input placeholder="选择" readonly style="width: 95px;"></el-input>
                    </div>
                    <div class="s margin_r10" v-if="type==3">
                      <!-- <el-input placeholder="填写" readonly style="width: 95px;"></el-input> -->
                      <el-select v-model="item.answer_id" placeholder="请选择选项" style="width: 95px;">
                        <el-option
                          v-for="item in answers"
                          :key="item.id"
                          :label="item.answer_title"
                          :value="item.id">
                        </el-option>
                      </el-select>
                    </div>
                    <div class="flex1" v-if="type!=3">
                      <el-select v-model="item.answer_id" placeholder="请选择选项" class="w_100">
                        <el-option
                          v-for="item in answers"
                          :key="item.id"
                          :label="item.answer_title"
                          :value="item.id">
                        </el-option>
                      </el-select>
                    </div>

                    <div class="flex1 flex_ac" v-if="type==3">
                      <el-select v-model="item.symbol" placeholder="请选择" class="w_100">
                        <el-option
                          v-for="item in symbols"
                          :key="item.symbol"
                          :label="item.title"
                          :value="item.symbol">
                        </el-option>
                      </el-select>
                      <el-input placeholder="数量" type="number" v-model="item.constant" style="width: 100%; margin-left: 10px;"></el-input>
                    </div>
                  </div>
                  <div class="s_item flex_ac">
                    <div style="width: 60px;text-align: right; padding-right:10px">则</div>
                    <div class="s margin_r10">
                      <el-input placeholder="跳转" readonly style="width: 95px;"></el-input>
                    </div>
                    <div class="flex1">
                      <el-select v-model="item.subject_id" placeholder="请选择题干" class="w_100">
                        <el-option
                          v-for="item in subjects"
                          :disabled="item.id==id"
                          :key="item.id"
                          :label="item.title"
                          :value="item.id">
                        </el-option>
                      </el-select>
                    </div>
                  </div>
                </div>
                <a href="javascript:;" class="el-button f15 el-button--text el-button--mini flex_ac" @click="handleDelete(index)" style="color: #FF5D5D;"><img src="@/assets/answer1.png" style="margin-right: 4px;" alt="">删除</a>
              </div>
            </div>
          </el-form>
        </div>
      </el-scrollbar>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open2=false;">取 消</el-button>
        <el-button type="primary" @click="submitForm('form2')" :loading="is_ajax">{{is_ajax?'保存中···':'确定'}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'setLogic',
  data () {
    return {
      checkList:[],
      value:'',
      openOptions: [{label:'当前页面打开',value:0},{label:'新页面打开',value:1}],
      form2: {},
      // 表单校验
      rules2: {

      },
      open2:false,
      is_ajax:false,
      logics:[],
      answers:[],
      subjects:[],
      type:'',
      symbols:[]
    }
  },
  created() {
  },
  methods:{
    init(row,q_id){
      this.id=row.id;
      this.type=row.type;
      this.q_id=q_id;
      this.form2={};
      this.open2=true;
      this.logics=[];

      this.get_data()
    },
    add_logic(){
      let obj={
        type:this.type==3?2:1,
        answer_id:'',
        subject_id:''
      }
      if(obj.type==2){
        obj.symbol='=='
      }
      this.logics.push(obj)
    },
    submitForm(formName) {
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
            for(let i=0;i<_this.logics.length;i++){
              if(_this.type!=3){
                if(!_this.logics[i].answer_id || !_this.logics[i].subject_id){
                  _this.tip_show('请完善逻辑设置','error')
                  return false;
                }
              }else{
                if(_this.logics[i].constant==='' || !_this.logics[i].subject_id){
                  _this.tip_show('请完善逻辑设置','error')
                  return false;
                }
              }
            }
            _this.is_ajax=true;
            let _data
            let tk_arr=[];
            if(_this.type!=3){
              _data={
                subject_id:_this.id,
                logic:[..._this.logics]
              }
            }else{
              let log=[]

              function inarr(id){
                let n=null;
                for(let k=0;k<log.length;k++){
                  if(log[k].answer_id==id){
                    n=k
                    break;
                  }
                }
                return n
              }
              for(let i=0;i<_this.logics.length;i++){
                let ind=inarr(_this.logics[i].answer_id);
                let tkobj={
                  symbol:_this.logics[i].symbol,
                  constant:_this.logics[i].constant,
                  subject_id:_this.logics[i].subject_id,
                }
                if(ind!=null){
                  log[ind].rules.push(tkobj)
                }else{
                  log.push({
                    answer_id:_this.logics[i].answer_id,
                    subject_id:'',
                    type:2,
                    rules:[tkobj]
                  })
                }
              }
              _data={
                subject_id:_this.id,
                logic:log
              }
            }

            console.log(_data);
            let url="/api/subject.LogicSet/set_jump_logic";
            _this.axios.post(url, _data).then(function(data) {
              if(data){
                _this.tip_show('操作成功','success');
                _this.$emit('after_logic')
                _this.open2=false;
              }
              _this.is_ajax=false;
            }).catch(function(error) {
              _this.is_ajax=false
              console.log(error);
            })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    get_data(){
      const _this=this;
      _this.axios.get("/api/subject.LogicSet/get_subject_list", {params:{q_id:_this.q_id,subject_id:_this.id}}).then(function(res) {
        if(res){
          let arr=res.message;
          // let s=[];
          // let is_find=false;
          // for(let i=0;i<arr.length;i++){
          //   if(is_find){
          //     s.push(arr[i])
          //   }else{
          //     if(arr[i].id==_this.id){
          //       is_find=true;
          //     }
          //   }
          // }
          _this.subjects=arr
        }
      }).catch(function(error) {
        console.log(error);
      })

      _this.axios.get("/api/subject.LogicSet/show_single_logic", {params:{subject_id:_this.id}}).then(function(res) {
        if(res){
            let list=res.message;
            if(list.length==0){
              _this.add_logic();
            }else{
              if(_this.type!=3){
                for(let i=0;i<list.length;i++){
                    _this.logics.push({
                      type:list[i].type,
                      answer_id:list[i].id,
                      subject_id:parseInt(list[i].subject_id)
                    })
                }
              }else{
                for(let i=0;i<list.length;i++){
                  for(let k=0;k<list[i].rules.length;k++){
                    _this.logics.push({
                      answer_id:list[i].id,
                      type:list[i].type,
                      subject_id:parseInt(list[i].rules[k].subject_id),
                      symbol:list[i].rules[k].symbol,
                      constant:list[i].rules[k].constant
                    })
                  }
                }
              }
            }

        }
      }).catch(function(error) {
        console.log(error);
      })

        _this.axios.get("/api/subject.LogicSet/get_subject_answer", {params:{subject_id:_this.id}}).then(function(res) {
          if(res){
            _this.answers=res.message
          }
        }).catch(function(error) {
          console.log(error);
        })
        _this.axios.get("/api/subject.LogicSet/expression_logic_data", {params:{subject_id:_this.id}}).then(function(res) {
          if(res){
            _this.symbols=res.message
          }
        }).catch(function(error) {
          console.log(error);
        })




    },
    /** 删除按钮操作 */
    handleDelete(index) {
      const _this=this;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          _this.logics.splice(index,1)
        })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

.set_win_content{color: #1F2329;}
.set_win_content .item{background: #F4F7FA;  border-radius: 4px; padding: 16px 20px; font-size: 15px;}
.set_win_content .set_d{background: #E4EAF0; padding:  10px 12px; padding-top: 16px;}
.add_d  img{width: 20px; height: 20px; margin-right: 6px;}
.set_win_content .s_item{padding: 5px 0;}
</style>
